package com.zhangxiaofanfan.hotcode.fourth.third;

/**
 *
 * @date 2024-07-13 03:14:13
 * @author zhangxiaofanfan
 */
public class HotCode079 {
    public static void main(String[] args) {
        HotCode079 hotCode = new HotCode079();
        int[] nums = {1,2,3};
        System.out.println(hotCode.jump(nums));
    }

    public int jump(int[] nums) {
        if (nums.length == 1) {
            return 0;
        }
        if (nums[0] >= nums.length - 1) {
            return 1;
        }
        int count = 0, maxIndex = nums[0], index = 1, curMaxIndex;
        while (true) {
            count++;
            curMaxIndex = maxIndex;
            while (index <= curMaxIndex) {
                maxIndex = Math.max(maxIndex, index + nums[index]);
                if (maxIndex >= nums.length - 1) {
                    return count + 1;
                }
                index++;
            }
        }
    }

}
